package me.xiaosiqi.server_info.controller;

import lombok.extern.slf4j.Slf4j;
import me.xiaosiqi.server_info.comm.BaseResponse;
import me.xiaosiqi.server_info.tool.IpTool;
import me.xiaosiqi.server_info.tool.ServerChanService;
import me.xiaosiqi.server_info.tool.ServerDescTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author xiaosiqi
 * @date 2021/11/20 19:33
 */
@Slf4j
@RestController
@RequestMapping("/command")
public class HomeController {

    @Autowired
    private ServerChanService serverChanService;
    //网卡名
    @Value("${spring.ethName}")
    private String ethName;

    @PostMapping("/getNetInfo")
    public BaseResponse getNetInfo() {
        try {
            Map<String, List<String>> ipInfo = IpTool.getIpInfo();
            List<String> ipList = ipInfo.get(ethName);
            String info = ServerDescTool.getNetInfo(ipList);
            serverChanService.sendMsg("服务器信息", info);
            return BaseResponse.ok();
        } catch (Exception e) {
            return BaseResponse.error(e.getMessage());
        }

    }

    @PostMapping("/getServerInfo")
    public BaseResponse getServerInfo() {
        try {
            String info = ServerDescTool.getServerInfo();
            serverChanService.sendMsg("服务器信息", info);
            return BaseResponse.ok();
        } catch (Exception e) {
            return BaseResponse.error(e.getMessage());
        }

    }
}
